package com.itheima.service.impl;


import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;

import java.util.List;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account){
        accountDao.update(account);
    }

    public void delete(Integer id) {
        accountDao.delete(id);
    }

    public Account findById(Integer id) {
        //System.out.println("GGBoss");
        return accountDao.findById(id);
    }
    public Double buy(Double money) {
        System.out.println("购买商品使用的价格:"+money);
        return money;
    }

    @Override
    public void addMoney(Integer id, Double money) {
        Account id1 = findById(id);
        Double money1 = id1.getMoney()+money;
        id1.setMoney(money1);
        update(id1);
    }

    @Override
    public void subMoney(Integer id, Double money) {
        Account id1 = findById(id);
        Double money1 = id1.getMoney()-money;
        id1.setMoney(money1);
        update(id1);
    }

    @Override
    public void transferMoney(Integer id, Integer sid, Double money) throws InterruptedException {
        addMoney(id,money);
        Thread thread = Thread.currentThread();
        thread.sleep(10000);
        //int i = 1/0;
        subMoney(sid,money);
        System.out.println("结束了");
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }
}
